package com.klangner.go.app;

import java.awt.AWTEventMulticaster;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;

import com.klangner.go.app.rules.GoConstants;


public class SettingsDialog extends Dialog {
  Panel panel1 = new Panel();
  Panel panel2 = new Panel();
  Panel panel3 = new Panel();
  Panel panel4 = new Panel();

  Label label1 = new Label();
  Label label2 = new Label();

  Choice choiceBlackPlayerType = new Choice();

  Choice choiceWhitePlayerType = new Choice();
  GridLayout gridLayout1 = new GridLayout();
  Panel panel5 = new Panel();
  Button btnCancel = new Button();
  Button btnOK = new Button();
  Label label3 = new Label();
  TextField edtBoardSize = new TextField();

  public SettingsDialog(Frame frame, String title, boolean modal) {
    super(frame, title, modal);

    thisListener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        SettingsDialog.this.setVisible(false);
        if (listeners != null) {
           // notify all registered listeners
          System.out.println("SettingsDialog: listeners notified - " +
                              e.getActionCommand());
          listeners.actionPerformed(new ActionEvent(SettingsDialog.this,
                                                    e.getID(),
                                                    e.getActionCommand()));
        }
      }
    };

    try  {
      jbInit();
      add(panel1);
      pack();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public SettingsDialog(Frame frame) {
    this(frame, "", false);
  }

  public SettingsDialog(Frame frame, boolean modal) {
    this(frame, "", modal);
  }

  public SettingsDialog(Frame frame, String title) {
    this(frame, title, false);
  }

  public String getBoardSize() {
    return edtBoardSize.getText();
  }

  public String getBlackPlayerType() {
    return choiceBlackPlayerType.getSelectedItem();
  }

  public String getWhitePlayerType() {
    return choiceWhitePlayerType.getSelectedItem();
  }

  /** This field will hold a list of registered ActionListeners, thanks
   *  to the magic of AWTEventMulticaster */
  protected ActionListener listeners = null;

  /** Register an action listener to be notified when a button is pressed
   *  AWTEventMulticaster makes this easy. */
  public void addActionListener(ActionListener l) {
    listeners = AWTEventMulticaster.add(listeners, l);
  }

  /** Remove an action listener from our list of interested listeners */
  public void removeActionListener(ActionListener l) {
    listeners = AWTEventMulticaster.remove(listeners, l);
  }

  void jbInit() throws Exception {
    panel1.setLayout(gridLayout1);
    label1.setText("BLACK");
    label2.setText("WHITE");
    choiceWhitePlayerType.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        choiceWhitePlayerType_itemStateChanged(e);
      }
    });
    gridLayout1.setRows(4);
    gridLayout1.setColumns(1);

    btnCancel.setLabel("Cancel");
    btnCancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        btnCancel_actionPerformed(e);
      }
    });


    label3.setText("Board size");


    btnOK.setLabel("  OK  ");
    btnOK.setActionCommand("ok");
    btnOK.addActionListener(thisListener);
    btnOK.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        btnOK_actionPerformed(e);
      }
    });

    choiceBlackPlayerType.addItem("Human player");
    choiceBlackPlayerType.addItem("Greedy Computer player");
    choiceBlackPlayerType.addItem("MinMax Computer player");
    choiceBlackPlayerType.addItem(GoConstants.ptAlphaBetaComputerPlayer);
    //choiceBlackPlayerType.addItem("TD Computer player");
    choiceBlackPlayerType.select(-1);

    choiceWhitePlayerType.addItem("Human player");
    choiceWhitePlayerType.addItem("Greedy Computer player");
    choiceWhitePlayerType.addItem("MinMax Computer player");
    choiceWhitePlayerType.addItem(GoConstants.ptAlphaBetaComputerPlayer);
    //choiceWhitePlayerType.addItem("TD Computer player");
    choiceWhitePlayerType.select(-1);

    panel1.add(panel4, null);
    panel4.add(label3, null);
    panel4.add(edtBoardSize, null);
    panel1.add(panel2, null);
    panel1.add(panel3, null);
    panel1.add(panel5, null);

    panel2.add(label1, null);
    panel2.add(choiceBlackPlayerType, null);

    panel3.add(label2, null);
    panel3.add(choiceWhitePlayerType, null);

    panel5.add(btnOK, null);
    panel5.add(btnCancel, null);
  }

  void cancel() {
    //dispose();
    setVisible(false);
  }

  void switchColors(ActionEvent e) {
   //todo: switch colors
  }

  void btnOK_actionPerformed(ActionEvent e) {
    //todo: register changes
    cancel();
  }

  void btnCancel_actionPerformed(ActionEvent e) {
    cancel();
  }

  void choiceWhitePlayerType_itemStateChanged(ItemEvent e) {
    this.setTitle(choiceWhitePlayerType.getSelectedItem());
  }

  private ActionListener thisListener;

}


